package me.bcloud.web.servlet.manager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.bcloud.web.domain.JobConfig;
import me.bcloud.web.domain.OnlineCounter;
import me.bcloud.web.service.JobConfigService;

import org.springframework.stereotype.Controller;

@Controller
public class ListServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	
	@Resource
	private JobConfigService jobConfigService;
	
	public void setJobConfigService(JobConfigService jobConfigService) {
		this.jobConfigService = jobConfigService;
	}


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<JobConfig> jobConfigs=jobConfigService.list();
	
		List<String> lastWriteList=new  ArrayList<String>();
		List<Integer> dayOfCountList=new  ArrayList<Integer>();
		List<Integer> countList=new  ArrayList<Integer>();
		for (JobConfig jobConfig : jobConfigs) {
			if (jobConfig.isOpen()) {
				lastWriteList.add(jobConfigService.getLastTime(jobConfig.getWriteTable()));
				dayOfCountList.add(jobConfigService.getDayOfCount(jobConfig.getWriteTable()));
				countList.add(jobConfigService.getCount(jobConfig.getWriteTable()));
			}else {
				lastWriteList.add("");
				dayOfCountList.add(-1);
				countList.add(-1);
			}
			
			
			
		}
		req.setAttribute("jobconfigs", jobConfigs);
		req.setAttribute("countList", countList);
		req.setAttribute("dayOfCountList", dayOfCountList);
		req.setAttribute("lastWriteList", lastWriteList);
		req.setAttribute("online", OnlineCounter.getOnline());
		req.getRequestDispatcher("list.jsp").forward(req, resp);


	}
}
